﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ZeroBug.PersonalityEvaluation
{
    public partial class EvaluationViewModel : ObservableObject
    {
        bool isCheckDone = false;
        [ObservableProperty]
        string questionInfo;
        [ObservableProperty]
        string questionNo;
        [ObservableProperty]
        bool isFirstQuestion = false;
        [ObservableProperty]
        bool isLastQuestion = false;
        [ObservableProperty]
        bool isFinishTest = false;
        [ObservableProperty]
        double questionProgress;
        [ObservableProperty]
        string currentAnswerA;
        [ObservableProperty]
        string currentAnswerB;
        [ObservableProperty]
        string nextTip = "下一题";
        [ObservableProperty]
        bool answerAChecked;
        [ObservableProperty]
        bool answerBChecked;
        [ObservableProperty]
        Person testPerson;

        private int _currentIndex;
        public int CurrentIndex
        {
            get { return _currentIndex; }
            set 
            { 
                _currentIndex = value;
                QuestionNo = $"当前第{_currentIndex}题";
                IsFirstQuestion = _currentIndex == 1;
                IsLastQuestion = _currentIndex == 30;
                CurrentAnswerA = questions.Where(q => q.QuestionIndex == _currentIndex).ToList()[0].Options[0];
                CurrentAnswerB = questions.Where(q => q.QuestionIndex == _currentIndex).ToList()[0].Options[1];
            }
        }

        private List<Question> questions;
        public EvaluationViewModel()
        {
            #region add question
            questions = new List<Question>();
            int index = 1;
            Dictionary<int, PersonalityType> answer2Personality;

            Question q = new Question(index, new List<string>()
            {
                "我倾向于告诉大家我的真实想法。",
                "我倾向于保留自己的真实想法。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.T);
            answer2Personality.Add(2, PersonalityType.A);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我是个内向的人，我与人相处总是很谨慎。",
                "我是个外向的人，我与人相处总是很热情。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.C);
            answer2Personality.Add(2, PersonalityType.E);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我习惯将我的情绪自然流露。",
                "我习惯控制我的情绪。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.E);
            answer2Personality.Add(2, PersonalityType.C);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我是一个性子急的人。",
                "我是一个性子慢的人。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.T);
            answer2Personality.Add(2, PersonalityType.A);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我通常做事喜欢冒险。",
                "我通常喜欢避免危险或减少危险。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.T);
            answer2Personality.Add(2, PersonalityType.A);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我喜欢轻松的生活。",
                "我喜欢有规律的生活。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.E);
            answer2Personality.Add(2, PersonalityType.C);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我常常考虑别人的意见后作出决定。",
                "我一般根据自己的意愿决定，不受别人的影响。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.A);
            answer2Personality.Add(2, PersonalityType.T);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我依据原则办事。",
                "我更看重事实。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.E);
            answer2Personality.Add(2, PersonalityType.C);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我更喜欢与别人一起做事。",
                "我更愿意独自分担一份工作。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.E);
            answer2Personality.Add(2, PersonalityType.C);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我更擅长控制局势，作开拓性的工作。",
                "我喜欢作支持者，作基础工作。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.T);
            answer2Personality.Add(2, PersonalityType.A);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我认为我为人处事开朗、直率。",
                "我认为我为人处事平静、温和。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.T);
            answer2Personality.Add(2, PersonalityType.A);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "大多情况，我更看重结果和效果。",
                "大多情况，我更重视过程和方法。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.T);
            answer2Personality.Add(2, PersonalityType.A);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我常对人们如何想问题更关注。",
                "我常对人们想什么更关注。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.E);
            answer2Personality.Add(2, PersonalityType.C);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我一般以随意的、轻松的方式与人交往。",
                "我喜欢较为有理性的、严谨的方式与人交往。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.E);
            answer2Personality.Add(2, PersonalityType.C);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "一般地，我处理问题避开个人行为的介入（我处理问题一视同仁）。",
                "我处理问题往往采用更多的个人化的方式解决（我因人而异处理问题）。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.C);
            answer2Personality.Add(2, PersonalityType.E);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "一般地，我选择面对问题（或矛盾）去处理事情。",
                "一般地，我选择避开问题（或矛盾）去处理事情。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.T);
            answer2Personality.Add(2, PersonalityType.A);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我做事比较急躁。",
                "我做事比较耐心。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.T);
            answer2Personality.Add(2, PersonalityType.A);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我喜欢与人相互交流意见。",
                "我愿意与人相互交流意见，但限定在一定的范围内。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.E);
            answer2Personality.Add(2, PersonalityType.C);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "一般地，我处理问题一般对事，不对人，更关注事。",
                "我处理问题往往将事与人的感受结合考虑，更看重关系。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.C);
            answer2Personality.Add(2, PersonalityType.E);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我主张与人交往应保持一定的距离。",
                "我直言不讳、坦诚以待与人交往。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.C);
            answer2Personality.Add(2, PersonalityType.E);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我认为人与人之间，更多的是竞争。",
                "我认为人与人之间，更多的是合作。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.T);
            answer2Personality.Add(2, PersonalityType.A);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "在讨论中，我喜欢保留自己的意见，保持沉默。",
                "在讨论中，我喜欢陈述自己的意见，说服别人。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.A);
            answer2Personality.Add(2, PersonalityType.T);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "一般，我不受别人情绪的影响。",
                "一般，我在意于别人的情绪。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.C);
            answer2Personality.Add(2, PersonalityType.E);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我习惯很快做出决定。",
                "我需要花多点时间作决定。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.T);
            answer2Personality.Add(2, PersonalityType.A);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我随和、我富有耐心。",
                "我对自己和别人的要求一样严格。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.E);
            answer2Personality.Add(2, PersonalityType.C);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我更象一个跟随者。",
                "我更象一个指导者。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.A);
            answer2Personality.Add(2, PersonalityType.T);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我是一个用词审慎的、规范的人。",
                "我是一个充满激情的、诗意的人。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.C);
            answer2Personality.Add(2, PersonalityType.E);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我一般保留我的观点和想法。",
                "我很高兴与别人分享我的观点。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.A);
            answer2Personality.Add(2, PersonalityType.T);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "在与人合作时，我争取按我的想法去作。",
                "在与人合作时，我一般接受别人的想法。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.T);
            answer2Personality.Add(2, PersonalityType.A);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            q = new Question(index, new List<string>()
            {
                "我喜欢与人分享我的感受。",
                "我一般保留自己的感受。"
            });
            answer2Personality = new Dictionary<int, PersonalityType>();
            answer2Personality.Add(1, PersonalityType.E);
            answer2Personality.Add(2, PersonalityType.C);
            q.Answer2Personality = answer2Personality;
            questions.Add(q);
            index++;

            #endregion
            CurrentIndex++;
        }
        [RelayCommand]
        void Move2Before()
        {
            CurrentIndex--;
            if (questions.Find(q => q.QuestionIndex == CurrentIndex).AnswerIndex == 0)
            {
                AnswerAChecked = false;
                AnswerBChecked = false;
            }
            else if (questions.Find(q => q.QuestionIndex == CurrentIndex).AnswerIndex == 1)
            {
                AnswerAChecked = true;
                AnswerBChecked = false;
            }
            else if (questions.Find(q => q.QuestionIndex == CurrentIndex).AnswerIndex == 2)
            {
                AnswerAChecked = false;
                AnswerBChecked = true;
            }
        }
        [RelayCommand]
        void Move2Next()
        {
            CurrentIndex++;
            if (questions.Find(q => q.QuestionIndex == CurrentIndex).AnswerIndex == 0)
            {
                AnswerAChecked = false;
                AnswerBChecked = false;
            }
            else if(questions.Find(q => q.QuestionIndex == CurrentIndex).AnswerIndex == 1)
            {
                AnswerAChecked = true;
                AnswerBChecked = false;
            }
            else if (questions.Find(q => q.QuestionIndex == CurrentIndex).AnswerIndex == 2)
            {
                AnswerAChecked = false;
                AnswerBChecked = true;
            }
        }
        [RelayCommand]
        void FinishEvaluation()
        {
            TestPerson.Score.AScore = questions.Count(q => q.CurrentAnswerType == PersonalityType.A);
            TestPerson.Score.TScore = questions.Count(q => q.CurrentAnswerType == PersonalityType.T);
            TestPerson.Score.CScore = questions.Count(q => q.CurrentAnswerType == PersonalityType.C);
            TestPerson.Score.EScore = questions.Count(q => q.CurrentAnswerType == PersonalityType.E);

            TestPerson.DoTestTime = DateTime.Now;

            Dictionary<string, object> oneRecord = new Dictionary<string, object>();
            oneRecord.Add("Name", TestPerson.Name);
            oneRecord.Add("AScore", TestPerson.Score.AScore);
            oneRecord.Add("TScore", TestPerson.Score.TScore);
            oneRecord.Add("CScore", TestPerson.Score.CScore);
            oneRecord.Add("EScore", TestPerson.Score.EScore);
            oneRecord.Add("TestTime", TestPerson.DoTestTime);
            App.Current.Services.GetService<IDatabase>()!.InsertOneRecord("Personality", oneRecord);

            MessageBox.Show("感谢您参与本次测评,祝您生活愉快!", "测评结束", MessageBoxButton.OK, MessageBoxImage.Information);
        }
        partial void OnAnswerACheckedChanged(bool oldValue, bool newValue)
        {
            if(newValue)
            {
                questions.Find(q => q.QuestionIndex == CurrentIndex).AnswerIndex = 1;
                QuestionInfo = $"已完成{questions.Count(q => q.AnswerIndex != 0)}/30";
                QuestionProgress = questions.Count(q => q.AnswerIndex != 0) * 1.0 / 30 * 100;
                if (_currentIndex == 30 || isCheckDone)
                {
                    isCheckDone = true;
                    if (CheckQuestionAllDone())
                        showFinishButton();
                }
            }
        }
        partial void OnAnswerBCheckedChanged(bool oldValue, bool newValue)
        {
            if (newValue)
            {
                questions.Find(q => q.QuestionIndex == CurrentIndex).AnswerIndex = 2;
                QuestionInfo = $"已完成{questions.Count(q => q.AnswerIndex != 0)}/30";
                QuestionProgress = questions.Count(q => q.AnswerIndex != 0) * 1.0 / 30 * 100;
                if (_currentIndex == 30 || isCheckDone)
                {
                    isCheckDone = true;
                    if (CheckQuestionAllDone())
                        showFinishButton();
                }
            }
        }
        public bool CheckQuestionAllDone()
        {
            List<Question> qs = questions.Where(q => q.AnswerIndex == 0).ToList();
            if (qs != null && qs.Count > 0)
            {
                Question q = qs.First();
                if (q != null)
                {
                    Task.Run(async () =>
                    {
                        await Task.Delay(400);
                        MessageBox.Show($"您还有未完成的题目,第{q.QuestionIndex}题!");
                        Jump2Question(q.QuestionIndex);
                    });
                    return false;
                }
                else
                    return true;
            }
            else
                return true;
        }
        public void Jump2Question(int questionIndex)
        {
            CurrentIndex = questionIndex;
            AnswerAChecked = false;
            AnswerBChecked = false;
        }
        public void showFinishButton()
        {
            IsFinishTest = true;
        }
    }

    public class Question
    {
        public int QuestionIndex { get; set; }
        private int _answerIndex;

        public int AnswerIndex
        {
            get { return _answerIndex; }
            set 
            { 
                _answerIndex = value; 
                CurrentAnswerType = Answer2Personality[_answerIndex];
            }
        }

        public List<string> Options { get; set; }
        public Dictionary<int, PersonalityType> Answer2Personality { get; set; }
        public PersonalityType CurrentAnswerType { get; set; }
        public Question(int qIndex, List<string> options)
        {
            QuestionIndex = qIndex;
            Options = options;
        }
    }
    public enum PersonalityType
    {
        A,
        T,
        C,
        E,
    }
}
